package com.dwork.base.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.dwork.base.controller.utils.RequestResponseContext;

public class BaseInterceptor extends HandlerInterceptorAdapter {

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if(modelAndView!=null) {
			String viewName = modelAndView.getViewName();
//	        if(viewName != null && !viewName.startsWith("redirect:")) {
//	            //笔者扩展的httpInclude
//	            modelAndView.addObject("httpInclude", new HttpInclude(request, response));
//	            
//	            //freemarker 原生的IncludePage指令
//	            modelAndView.addObject("include_page", new IncludePage(request, response));
//	        }
		}
        super.postHandle(request, response, handler, modelAndView);

	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.handler.HandlerInterceptorAdapter#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		/*
		if(handler instanceof HandlerMethod) {
			HandlerMethod method = (HandlerMethod) handler;
			if(method.getBean() instanceof BaseController) {
				BaseController bb = (BaseController) method.getBean();
				bb.setRequest(request);
				bb.setResponse(response);
			}
		}*/
		RequestResponseContext.setRequest(request);
		RequestResponseContext.setResponse(response);
		return super.preHandle(request, response, handler);
	}

	
	
	
}
